package com.wzdigit.wms.wms.controller.inv;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.inv.SealGoodsRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchQualifiedGoodsRequest;
import com.wzdigit.wms.basic.client.request.inv.SearchSealGoodsGroupRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.inv.QualifiedGoodsResponse;
import com.wzdigit.wms.basic.client.response.inv.SealGoodsGroupResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.inv.SealGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 封库/解封明细货品清单 前端控制器
 * </p>
 *
 * @author 
 * @since 2021-06-05
 */
@RestController
@Api(tags = "封库")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class SealGoodsController {

	public static final String REQUEST_MODEL = "/sealgoods";
	
	@Autowired
	private SealGoodsService sealGoodsService;

	/**
	 * 【封库主档数据】输出
	 * @param dto
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "【封库主档数据】输出",notes = "【封库主档数据】输出")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchSealGoodsGroup")
    public PagerInfo<List<SealGoodsGroupResponse>> searchSealGoodsGroup(
    		SearchSealGoodsGroupRequest dto, Pager<?> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return sealGoodsService.searchSealGoodsGroup(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 【封库合格SN数据】输出
	 * @param dto
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "【封库合格SN数据】输出",notes = "【封库合格SN数据】输出")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchQualifiedGoods")
    public PagerInfo<List<QualifiedGoodsResponse>> searchQualifiedGoods(
    		SearchQualifiedGoodsRequest dto, Pager<?> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return sealGoodsService.searchQualifiedGoods(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 封库
	 * @param dto
	 * @param bindingResult
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation(value = "封库",notes = "封库")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/sealGoods")
    public Result<String> sealGoods(
    		@RequestBody @Valid SealGoodsRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
        	ValidatorUtil.validator(bindingResult);
            return sealGoodsService.sealGoods(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
